var txtcurlatitude = {};
var txtcurlongitude = {};
var txtBaiducurlatitude={};
var txtBaiducurlongitude={};
var txtfixlatitude={};
var txtfixlongitude={};
var txtDistance={};
function getLocation(){
	//alert(navigator.geolocation);
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(locationSuccess, locationError,{
			// 指示浏览器获取高精度的位置，默认为false
			enableHighAcuracy: true,

			// 指定获取地理位置的超时时间，默认不限时，单位为毫秒
			timeout: 5000,
        
			// 最长有效期，在重复获取地理位置时，此参数指定多久再次获取位置。
			maximumAge: 3000
		});
	}else{
		alert("Your browser does not support Geolocation!");
	}
}
  

function locationError(error){
//    alert("error.code:"+error.code);
//    alert("error.message:"+error.message);
//    alert("error.TIMEOUT:"+error.TIMEOUT);
    switch(error.code) {
        case error.TIMEOUT:
            alert("A timeout occured! Please try again!");
            break;
        case error.POSITION_UNAVAILABLE:
            alert('We can\'t detect your location. Sorry!');
            break;
        case error.PERMISSION_DENIED:
            alert('Please allow geolocation access for this to work.');
            break;
        case error.UNKNOWN_ERROR:
            alert('An unknown error occured!');
            break;
    }

}
function locationSuccess(position){
	//alert("Latitude: " + position.coords.latitude+"\n"+"Longitude: " + position.coords.longitude);
	txtcurlatitude.value=position.coords.latitude;
	txtcurlongitude.value=position.coords.longitude;
	
	var point = {lat:position.coords.latitude,lng:position.coords.longitude};
	
	Convert_GCJ02_To_BD09(point);
	
	txtBaiducurlatitude.value=point.lat;
	txtBaiducurlongitude.value=point.lng;
}


function getDistance(){
	//alert("1");
	//txtDistance.value=getFlatternDistance(txtcurlatitude.value,txtcurlongitude.value,txtfixlatitude.value,txtfixlongitude.value);
	//txtDistance.value=getGreatCircleDistance(txtcurlatitude.value,txtcurlongitude.value,txtfixlatitude.value,txtfixlongitude.value);
	txtDistance.value=getGreatCircleDistance(txtBaiducurlatitude.value,txtBaiducurlongitude.value,txtfixlatitude.value,txtfixlongitude.value);
	return txtDistance.value;
}


function getFlatternDistance(){
//alert("1");
//  txtDistance.value=getFlatternDistance(txtcurlatitude.value,txtcurlongitude.value,txtfixlatitude.value,txtfixlongitude.value);
	txtFlatternDistance.value=getFlatternDistance(txtcurlatitude.value,txtcurlongitude.value,txtfixlatitude.value,txtfixlongitude.value);
}

var EARTH_RADIUS = 6378137.0;    //单位M
var PI = Math.PI;
    
function getRad(d){
    return d*PI/180.0;
}

function getGreatCircleDistance(lat1,lng1,lat2,lng2){
    var radLat1 = getRad(lat1);
    var radLat2 = getRad(lat2);
        
    var a = radLat1 - radLat2;
    var b = getRad(lng1) - getRad(lng2);
        
    var s = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s*EARTH_RADIUS;
    s = Math.round(s*10000)/10000.0;
                
    return s;
}

function getFlatternDistance(lat1,lng1,lat2,lng2){
    var f = getRad((lat1 + lat2)/2);
    var g = getRad((lat1 - lat2)/2);
    var l = getRad((lng1 - lng2)/2);
        
    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);
        
    var s,c,w,r,d,h1,h2;
    var a = EARTH_RADIUS;
    var fl = 1/298.257;
        
    sg = sg*sg;
    sl = sl*sl;
    sf = sf*sf;
        
    s = sg*(1-sl) + (1-sf)*sl;
    c = (1-sg)*(1-sl) + sf*sl;
        
    w = Math.atan(Math.sqrt(s/c));
    r = Math.sqrt(s*c)/w;
    d = 2*w*a;
    h1 = (3*r -1)/2/c;
    h2 = (3*r +1)/2/s;

    alert('l:'+l+'\nfl:'+fl+'\nh1:'+h1+'\nh2:'+h2+'\nsf:'+sf+'\nsg:'+sg);

    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}
    
var x_pi= 3.14159265358979324 * 3000.0 / 180.0;

    /// <summary>

    /// 中国正常坐标系GCJ02协议的坐标，转到 百度地图对应的 BD09 协议坐标

    ///  point 为传入的对象，例如{lat:xxxxx,lng:xxxxx}

    /// </summary>
function Convert_GCJ02_To_BD09 (point) {
    var x = point.lng, y = point.lat;
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
//        point.lng = z * Math.cos(theta) + 0.0065;
//        point.lat = z * Math.sin(theta) + 0.006;
///再次纠偏dxf
    point.lng = z * Math.cos(theta) + 0.0065 + 0.00525;
    point.lat = z * Math.sin(theta) + 0.006 - 0.0023;
}

    /// <summary>

    /// 百度地图对应的 BD09 协议坐标，转到 中国正常坐标系GCJ02协议的坐标

    /// </summary>

function Convert_BD09_To_GCJ02(point) {
//        var x = point.lng - 0.0065, y = point.lat - 0.006;
///再次纠偏dxf
    var x = point.lng - 0.0065 - 0.00525, y = point.lat - 0.006 - 0.0023;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    point.lng = z * Math.cos(theta);
    point.lat = z * Math.sin(theta);
}